﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citi.Helpers;
using Citi.Scheduler.Domain;
using Citi.Scheduler.WebUi.Helpers;
using Citi.Scheduler.WebUi.Models;

namespace Citi.Scheduler.WebUi.Controllers
{
    public class RoutineExecutionHistoryController : ControllerBase<RoutineExecutionHistoryModel, RoutineExecutionHistory>
    {
        public ActionResult Index(int routineExecutionId, bool showErrors = false)
        {
            var models = GetIndexModels(routineExecutionId).ToList();
            ViewBag.ShowErrors = showErrors;

            return View(models);
        }

        private static IEnumerable<RoutineExecutionHistoryModel> GetIndexModels(int routineExecutionId)
        {
            if (routineExecutionId == 0)
                return new List<RoutineExecutionHistoryModel> { new RoutineExecutionHistoryModel { Date = DateTime.Today, Details = "Scheduled" } };

            var dominios = new RoutineExecutionHistory().SelectAllActive<RoutineExecutionHistory>().Where(x => x.RoutineExecutionId == routineExecutionId);

            return dominios.Any()
                       ? new RoutineExecutionHistoryModel().CreateModel(dominios)
                       : new List<RoutineExecutionHistoryModel> { new RoutineExecutionHistoryModel { Date = DateTime.Today, Details = "Scheduled" } };
        }

        [ValidateInput(false)]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public JsonResult IndexJson(int routineExecutionId, bool showErrors = false)
        {
            var models = GetIndexModels(routineExecutionId);
            ViewBag.ShowErrors = showErrors;

            var chart = GetMemoryUsageChart(null, null, 0, routineExecutionId);

            return Json(new
            {
                html = GetControlData("~/Views/RoutineExecutionHistory/Index.cshtml", models),
                categories = chart.Categories,
                series = chart.Series
            }, JsonRequestBehavior.AllowGet);
        }

        public HighChartsHelper.ChartInformation GetMemoryUsageChart(DateTime? start = null, DateTime? end = null, int machineId = 0, int routineExecutionId = 0)
        {
            var execution = routineExecutionId != 0 ? new RoutineExecution().SelectById<RoutineExecution>(routineExecutionId) : null;
            var path = Path.Combine(MemoryUsagePath, (machineId == 0 ? execution.MachineId : machineId).ToString(), "{0}.xml");
            var memoryUsages = new Dictionary<RoutineExecution, IEnumerable<MemoryUsage>>();

            if (routineExecutionId == 0)
            {

            }
            else
            {
                path = string.Format(path, routineExecutionId);
                
                if (System.IO.File.Exists(path))
                    memoryUsages.Add(execution, XmlHelper.DeserializeFromXml<List<MemoryUsage>>(path));
            }

            return ChartsHelper.GetMemoryUsageChart(memoryUsages);
        }
    }
}
